package com.kzy.demoadmin.web.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.kzy.demoadmin.web.entity.Audit;
import com.kzy.demoadmin.web.entity.Audit;
import com.kzy.demoadmin.web.exception.BizException;
import com.kzy.demoadmin.web.mapper.AuditMapper;
import com.kzy.demoadmin.web.service.AuditService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class AuditServiceImpl implements AuditService {

    @Autowired
    private AuditMapper auditMapper;

    /**
     * 根据id查询
     * @param integer
     * @return
     */
    @Override
    public Audit findById(Integer integer) {
        return auditMapper.findById(integer);
    }

    /**
     * 分页查询
     * @param audit
     * @return
     */
    @Override
    public PageInfo<Audit> getAuditList(Audit audit) {
        PageHelper.startPage(audit.getPageNo(), audit.getPageSize());
        List<Audit> list = auditMapper.searchBy(audit);
        return new PageInfo<>(list);
    }

    /**
     * 删除审批
     * @param id
     * @return
     */
    @Override
    public int deleteById(Integer id) {
        return auditMapper.deleteById(id);
    }

    /**
     * 新增审批
     * @param audit
     */
    @Override
    public void add(Audit audit) {
        //1、请假理由不能为空
        if (audit.getName() == null || "".equals(audit.getName())){
            throw new BizException("请假理由不能为空");
        }

        //2、请假时间不能为空
        if (audit.getTime() == null || "".equals(audit.getTime())){
            throw new BizException("请假时间不能为空");
        }
        //3、请假天数不能为空
        if (audit.getDay() == null || "".equals(audit.getDay())){
            throw new BizException("请假天数不能为空");
        }
        //4、请假人id不能为空
        if (audit.getUserId() == null){
            throw new BizException("请假人不能为空");
        }
        //5、审批状态默认待审核
        audit.setStatus(1);

        auditMapper.insert(audit);
    }

    /**
     * 修改审批
     * @param audit
     */
    @Override
    public void edit(Audit audit) {
        //缘由不能为空
        if (audit.getName() == null || "".equals(audit.getName())){
            throw new BizException("请假缘由不能为空");
        }

        //请假时间不能为空
        if (audit.getTime() == null || "".equals(audit.getTime())){
            throw new BizException("请假时间不能为空");
        }

        //请假天数不能为空
        if (audit.getDay() == null || "".equals(audit.getDay())){
            throw new BizException("请假天数不能为空");
        }

        //请假人不能为空
        if (audit.getUserId() == null || "".equals(audit.getUserId())){
            throw new BizException("请假人不能为空");
        }

        //审批状态不能为空
        if (audit.getStatus() == null || "".equals(audit.getStatus())){
            throw new BizException("审批状态不能为空");
        }

        //5、修改审批
        auditMapper.update(audit);
    }
}
